/**
 *  Copyright (c) Filipe Manana <fdmanana@gmail.com>
 *  All rights reserved.
 * 
 *  This file is part of jsdet.
 *
 *  jsdet is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published
 *  by the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  jsdet is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with jsdet.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


package jsdet.parsing.regexps;


import java.util.regex.Pattern;
import java.util.regex.Matcher;


/**
 * Class to be used only by the JavaScriptParser class (ANTLR generated)
 * and its helper (jsdet.parsing.helper.JavaScriptParserHelper).
 * 
 * @author Filipe Manana
 *
 */
public class Regexps {

    // e.g.  varName  or   obj.prop
    public static final Pattern IDENT_INDEXED_PATTERN =
        Pattern.compile("^ [\\w$]+ (?: \\. [\\w$]+ )* $", Pattern.COMMENTS);

    // e.g.  this.id = whatever
    public static final Pattern THIS_MEMBER_PATTERN = Pattern.compile("^ this \\. ([\\w$]+) $", Pattern.COMMENTS);

    // e.g.  myapp.widget.Cart.prototype.init = function(...) { ... }
    public static final Pattern PROTO_MEMBER_PATTERN =
        Pattern.compile("^ ( (?:[\\w$]+ \\.)* [\\w$]+ ) \\. prototype \\. ([\\w$]+) $", Pattern.COMMENTS);

    // e.g. myapp.widget.Cart.prototype.config.labels
    public static final Pattern COMPLEX_PROTO_MEMBER_PATTERN =
        Pattern.compile("^ ( (?:[\\w$]+ \\.)* [\\w$]+ ) \\. prototype (\\. ([\\w$]+)){2,} $",  Pattern.COMMENTS);

    // e.g. myapp.MyClass.prototype = { ... }
    public static final Pattern PROTO_PATTERN =
        Pattern.compile("^ ( (?:[\\w$]+ \\.)* [\\w$]+ ) \\. prototype $", Pattern.COMMENTS);


    // Patterns specific for Ext JS functions / features

    public static final Pattern EXT_EMPTYFN = Pattern.compile("^ Ext \\. emptyFn $", Pattern.COMMENTS);

    public static final Pattern EXT_EXTEND = Pattern.compile("^ Ext \\. extend $", Pattern.COMMENTS);


    // convenience helper method
    public static boolean matches(String str, Pattern pattern) {
        Matcher m = pattern.matcher(str);

        return m.matches();
    }
}
